package com.jx.lcs.feedbackinfo.dao;

import java.util.List;
import org.hibernate.SQLQuery;
import org.hibernate.type.StandardBasicTypes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;
import com.tianyu.jty.common.persistence.HibernateDao;
import com.tianyu.jty.common.persistence.Page;
import com.jx.lcs.feedbackinfo.entity.FeedBackInfo;


/**
 * 用户DAO
 * @author ty
 * @date 2015年1月13日
 */
@Repository
public class FeedBackInfoDao extends HibernateDao<FeedBackInfo, String>{

	private static final Logger logger = LoggerFactory.getLogger(FeedBackInfoDao.class);

	/**
	 * 通过理财师姓名查询反馈信息
	 * @param userName 理财师姓名
	 * @return 反馈信息列表
	 */
	@SuppressWarnings("unchecked")
	public List<FeedBackInfo> getFeedBackInfoByName(String userName,Page<FeedBackInfo> page) {
		try{
				int pageNo = page.getPageNo();
				int pageSize = page.getPageSize();
				StringBuffer sb = new StringBuffer();
				sb.append("select t1.* from tb_feedback_info t1,tb_xh_employee t2 where t1.uid=t2.usercode");
				if(userName!=null && !userName.equals("")){
					sb.append(" and t2.username like '%").append(userName).append("%'");
				}
				sb.append(" order by recordtime desc limit ?0, ?1");
				SQLQuery sqlQuery = createSQLQuery(sb.toString(),(pageNo-1)*pageSize,pageSize);
				sqlQuery.addEntity(FeedBackInfo.class);
				return sqlQuery.list();
			
		} catch(Exception e) {
			logger.error("--通过理财师姓名查询反馈信息异常！", e);
			return null;
		}
	}

	/**
	 * 通过理财师姓名查询反馈信息条数
	 * @param userName 理财师姓名
	 * @return 反馈条数
	 */
	public int getFeedBackInfoNum(String userName) {
		try{
			StringBuffer sb = new StringBuffer();
			sb.append("select count(*) num from tb_feedback_info t1,tb_xh_employee t2 where t1.uid=t2.usercode");
			if(userName!=null && !userName.equals("")){
				sb.append(" and t2.username like '%").append(userName).append("%'");
			}
			SQLQuery sqlQuery = createSQLQuery(sb.toString());
			sqlQuery.addScalar("num",StandardBasicTypes.INTEGER );
			return ((Integer)(sqlQuery.uniqueResult())).intValue();
			
		} catch(Exception e) {
			logger.error("--通过理财师姓名查询反馈信息条数异常！", e);
			return 0;
		}
	}
}
